package com.wljy.developtool.service.impl;

import com.wljy.developtool.dto.QpsQueryDTO;
import com.wljy.developtool.dto.QpsQueryDetailsDTO;
import com.wljy.developtool.dto.QpsQueryResultDTO;
import com.wljy.developtool.entity.MetricApiEntity;
import com.wljy.developtool.entity.MetricIpEntity;
import com.wljy.developtool.mapper.MetricApiMapper;
import com.wljy.developtool.mapper.MetricIpMapper;
import com.wljy.developtool.service.IMetricService;
import com.wljy.developtool.vo.QpsAddVO;
import com.wljy.developtool.vo.QpsQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class MetricServiceImpl implements IMetricService {

    @Autowired
    private MetricApiMapper metricApiMapper;

    @Autowired
    private MetricIpMapper metricIpMapper;

    @Override
    public void qpsAdd(QpsAddVO qpsAddVO) {
        Object lock = org.zhouyi.nativedemo.utils.LockUtil.getLock(qpsAddVO.getAppName());
        synchronized (lock) {
            String appName = qpsAddVO.getAppName();
            Map<String, AtomicInteger> ipQps = qpsAddVO.getIpQps();
            Map<String, AtomicInteger> resourceQps = qpsAddVO.getResourceQps();
            ipQps.forEach((key, value) -> {
                MetricIpEntity metricIpEntity = new MetricIpEntity();
                metricIpEntity.setApp(appName);
                metricIpEntity.setCount(value.get());
                metricIpEntity.setResource(key);
                metricIpEntity.setTimestamp(new Date());
                metricIpEntity.setGmtCreate(new Date());
                metricIpMapper.insert(metricIpEntity);
            });

            resourceQps.forEach((key, value) -> {
                MetricApiEntity metricApiEntity = new MetricApiEntity();
                metricApiEntity.setApp(appName);
                metricApiEntity.setCount(value.get());
                metricApiEntity.setResource(key);
                metricApiEntity.setTimestamp(new Date());
                metricApiEntity.setGmtCreate(new Date());
                metricApiMapper.insert(metricApiEntity);
            });
        }

    }


    @Override
    public QpsQueryResultDTO qpsQuery(QpsQueryVO qpsQueryVO) {
        if (1 == qpsQueryVO.getType()) {
            //查ip
            List<QpsQueryDTO> qpsQueryDTO = metricIpMapper.qpsQuery(qpsQueryVO);
            List<String> dayTimes = qpsQueryDTO.stream().map(QpsQueryDTO::getDayTime).collect(Collectors.toList());
            List<QpsQueryDetailsDTO> qpsQueryDetailsList = new ArrayList<>();
            for (String dayTime : dayTimes) {
                List<QpsQueryDetailsDTO> qpsQueryDetails = metricIpMapper.getQpsByDayTime(dayTime);
                if (qpsQueryDetails.size() > 0) {
                    qpsQueryDetailsList.addAll(qpsQueryDetails);
                }
            }

            QpsQueryResultDTO qpsQueryResultDTO = new QpsQueryResultDTO();
            qpsQueryResultDTO.setQpsQueryDTO(qpsQueryDTO);
            qpsQueryResultDTO.setQpsQueryDetailsList(qpsQueryDetailsList);
            return qpsQueryResultDTO;
        }

        if (2 == qpsQueryVO.getType()) {
            //查API
            List<QpsQueryDTO> qpsQueryDTO = metricApiMapper.qpsQuery(qpsQueryVO);
            List<String> dayTimes = qpsQueryDTO.stream().map(QpsQueryDTO::getDayTime).collect(Collectors.toList());
            List<QpsQueryDetailsDTO> qpsQueryDetailsList = new ArrayList<>();
            for (String dayTime : dayTimes) {
                List<QpsQueryDetailsDTO> qpsQueryDetails = metricApiMapper.getQpsByDayTime(dayTime);
                if (qpsQueryDetails.size() > 0) {
                    qpsQueryDetailsList.addAll(qpsQueryDetails);
                }
            }
            QpsQueryResultDTO qpsQueryResultDTO = new QpsQueryResultDTO();
            qpsQueryResultDTO.setQpsQueryDTO(qpsQueryDTO);
            qpsQueryResultDTO.setQpsQueryDetailsList(qpsQueryDetailsList);
            return qpsQueryResultDTO;
        }
        return null;
    }

    @Override
    public List<QpsQueryDTO> appQps(String app) {
        LocalDateTime beginTime = LocalDateTime.now().minusHours(8);
        return metricApiMapper.appQps(app, beginTime, null);
    }
}
